# -*- coding:utf-8 -*-
'''
Created on 2019年7月13日

@author: Administrator
'''
import pygame as pg
import sys
import tkinter as tk
from tkinter import *
import tkinter.filedialog,tkinter.messagebox
from threading import Thread
import time,os,re
def gettime():
    t=time.strftime('%Y%m%d%H%M%S',time.localtime())
    return t

def on_closing():
    if tkinter.messagebox.askokcancel("退出", "你确定要退出吗?"):
        root.destroy()
        os._exit(1)

root = tk.Tk()
embed = tk.Frame(root, width = 900, height = 764) #creates embed frame for pygame window
embed.grid(columnspan = (600), rowspan = 500) # Adds grid
embed.pack(side = LEFT) #packs window to the left
root.title('中国象棋')
root.resizable(False,False)
root.protocol("WM_DELETE_WINDOW", on_closing)
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
#棋子65
filename=tkinter.StringVar()
countqipu=0
step=0
def openfile():
    global filename
    file=tkinter.filedialog.askopenfilename()
    filename.set(file)
    file_name=os.path.basename(file)
    compile=re.compile(r'\d{14}')
    content=compile.search(file_name)
    if content:
        restart()
        tkinter.messagebox.showinfo('提示', "载入棋谱成功")
    else:
        tkinter.messagebox.showerror('提示', "载入棋谱失败")
def writeqipu():
    global countqipu
    if countqipu==0:
        with open('{}'.format(gettime()+'.txt'),'w') as f:
            f.write(str(selectlist)+'\n'+str(chesslist)+'\n'+str(value_list)+'\n'+str(ischizi_list)+'\n'+str(death_value))
            countqipu+=1

def upstep():
    global step,countqipu,running
    running=False
    if filename.get()!='':
        try:
            print('开始上一步')
            with open(filename.get(),'r') as f:
                info=f.readlines()
                for i,j in enumerate(info):
                    info[i]=j.replace('\n','')
            select_list=eval(info[0])
            chess_list=eval(info[1])
            valuelist=eval(info[2])
            ischizilist=eval(info[3])
            deathvalue_list=eval(info[4])
            countqipu+=1
            print(info)
            if step>0:
                selecttuple=select_list[step-1]
                chesstuple=chess_list[step-1]
                chessvalue=valuelist[step-1]
                czpd=ischizilist[step-1]
                deathvalue=deathvalue_list[step-1]
                if czpd:
                    list[chesstuple[0]][chesstuple[1]]=deathvalue
                    list[selecttuple[0]][selecttuple[1]] = chessvalue
                else:
                    list[chesstuple[0]][chesstuple[1]]=0
                    list[selecttuple[0]][selecttuple[1]] = chessvalue
                step -= 1
        except Exception as e:
            print(e)
    else:
        print('未载入棋谱')



def downstep():
    global step,countqipu,running
    running=False
    if filename.get() != '':
        print('开始下一步')
        try:
            with open(filename.get(),'r') as f:
                info=f.readlines()
                for i,j in enumerate(info):
                    info[i]=j.replace('\n','')
            select_list=eval(info[0])
            chess_list=eval(info[1])
            valuelist=eval(info[2])
            print(info)
            countqipu+=1
            print(step)
            # if running:
            step += 1
            if step>0:
                selecttuple=select_list[step-1]
                chesstuple=chess_list[step-1]
                chessvalue=valuelist[step-1]
                print(chessvalue,chesstuple[0],chesstuple[1])
                list[selecttuple[0]][selecttuple[1]]=0
                list[chesstuple[0]][chesstuple[1]]=chessvalue
        except Exception as e:
            print(e)
    else:
        print('未载入棋谱')

def auto():
    global step,countqipu,running
    running=False
    button2.config(state=tkinter.DISABLED)
    button3.config(state=tkinter.DISABLED)
    if filename.get() != '':
        # if running:
        try:
            print('开始下一步')
            with open(filename.get(),'r') as f:
                info=f.readlines()
                for i,j in enumerate(info):
                    info[i]=j.replace('\n','')
            select_list=eval(info[0])
            chess_list=eval(info[1])
            valuelist=eval(info[2])
            countqipu+=1
            step += 1
            if step>0:
                selecttuple=select_list[step-1]
                chesstuple=chess_list[step-1]
                chessvalue=valuelist[step-1]
                print(chessvalue,chesstuple[0],chesstuple[1])
                list[selecttuple[0]][selecttuple[1]]=0
                list[chesstuple[0]][chesstuple[1]]=chessvalue
            button4.after(1000, auto)
        except Exception as e:
            print(e)
    else:
        print('未载入棋谱')
def restart():
    global step,receivechessx,receivechessx,recelistx,recelisty,running, flagred, flagblack, countqipu, redpaox, redpaoy, redpao2x, redpao2y, blackpaox, blackpaoy, blackpao2x, blackpao2y, blackmax, blackmay, blackma2x, blackma2y, redmax, redmay, redma2x, redma2y, redbingx, redbingy, redbing2x, redbing2y, redbing3x, redbing3y, redbing4x, redbing4y, redbing5x, redbing5y, blackchex, blackchey, blackzux, blackzuy, blackzu2x, blackzu2y, blackzu5x, blackzu5y, blackzu3x, blackzu3y, blackzu4x, blackzu4y, blackche2x, blackche2y, redchex, redchey, redche2x, redche2y, blackshix, blackshiy, redshix, redshiy, blackshi2x, blackshi2y, redshi2x, redshi2y, blackxiangx, blackxiangy, redxiangx, redxiangy, redxiang2x, redxiang2y, blackxiang2x, blackxiang2y, blackjiangx, blackjiangy, redshuaix, redshuaiy
    button4.config(state=tkinter.NORMAL)
    button2.config(state=tkinter.NORMAL)
    button3.config(state=tkinter.NORMAL)
    step = 0
    countqipu = 0
    ischizi_list.clear()
    value_list.clear()
    death_value.clear()
    selectlist.clear()
    chesslist.clear()
    clear()
    redselectlist.clear()
    blackselectlist.clear()
    # redchesslist.clear()
    # blackchesslist.clear()
    tranred = True
    running = True
    redpaox, redpaoy = 1, 7
    redpao2x, redpao2y = 7, 7
    blackpaox, blackpaoy = 1, 2
    blackpao2x, blackpao2y = 7, 2
    blackmax, blackmay = 7, 0
    blackma2x, blackma2y = 1, 0
    redmax, redmay = 1, 9
    redma2x, redma2y = 7, 9
    redbingx, redbingy = 6, 6
    redbing2x, redbing2y = 4, 6
    redbing3x, redbing3y = 2, 6
    redbing4x, redbing4y = 0, 6
    redbing5x, redbing5y = 8, 6
    blackchex, blackchey = 8, 0
    blackzux, blackzuy = 0, 3
    blackzu2x, blackzu2y = 2, 3
    blackzu5x, blackzu5y = 4, 3
    blackzu3x, blackzu3y = 6, 3
    blackzu4x, blackzu4y = 8, 3
    blackche2x, blackche2y = 0, 0
    redchex, redchey = 0, 9
    redche2x, redche2y = 8, 9
    blackshix, blackshiy = 3, 0
    redshix, redshiy = 3, 9
    blackshi2x, blackshi2y = 5, 0
    redshi2x, redshi2y = 5, 9
    blackxiangx, blackxiangy = 2, 0
    redxiangx, redxiangy = 2, 9
    redxiang2x, redxiang2y = 6, 9
    blackxiang2x, blackxiang2y = 6, 0
    blackjiangx, blackjiangy = 4, 0
    redshuaix, redshuaiy = 4, 9
    fangqi()
button1 = Button(root,width=8,text='载入棋谱',command=openfile,fg='blue')
button1.place(relx=0.8,rely=0.8)
button4 = Button(root,width=8,text='自动模式',command=auto,fg='blue')
button4.place(relx=0.9,rely=0.8)
button2 = Button(root,width=8,text='上一步',command=upstep,fg='blue')
button2.place(relx=0.8,rely=0.9)
button3 = Button(root,width=8,text='下一步',command=downstep,fg='blue')
button3.place(relx=0.9,rely=0.9)
root.update()
pg.init()
width=692
size=width2,height=900,764
screen=pg.display.set_mode(size)
pg.display.set_caption('中国象棋')
pg.display.update()
font=pg.font.SysFont('SimHei',60)
font2=pg.font.SysFont('SimHei',50)
font3=pg.font.SysFont('SimHei',40)
board=pg.image.load('.//ChineseChessGame//chessBoard.png')
blackma=pg.image.load('.//ChineseChessGame//blackma.png').convert_alpha()
blackpao=pg.image.load('.//ChineseChessGame//blackpao.png').convert_alpha()
redpao=pg.image.load('.//ChineseChessGame//redpao.png').convert_alpha()
blackche=pg.image.load('.//ChineseChessGame//blackche.png').convert_alpha()
blackjiang=pg.image.load('.//ChineseChessGame//blackjiang.png').convert_alpha()
blackpao=pg.image.load('.//ChineseChessGame//blackpao.png').convert_alpha()
blackshi=pg.image.load('.//ChineseChessGame//blackshi.png').convert_alpha()
blackzu=pg.image.load('.//ChineseChessGame//blackzu.png').convert_alpha()
blackxiang=pg.image.load('.//ChineseChessGame//blackxiang.png').convert_alpha()
redche=pg.image.load('.//ChineseChessGame//redche.png').convert_alpha()
redbing=pg.image.load('.//ChineseChessGame//redbing.png').convert_alpha()
redshuai=pg.image.load('.//ChineseChessGame//redshuai.png').convert_alpha()
redshi=pg.image.load('.//ChineseChessGame//redshi.png').convert_alpha()
redxiang=pg.image.load('.//ChineseChessGame//redxiang.png').convert_alpha()
redma=pg.image.load('.//ChineseChessGame//redma.png').convert_alpha()
huiqi=pg.image.load('.//ChineseChessGame//huiqi.png').convert_alpha()
xinju=pg.image.load('.//ChineseChessGame//xinju.png').convert_alpha()
background=(201,202,187)
red_pao=redpaox,redpaoy=1,7
red_pao2=redpao2x,redpao2y=7,7
black_pao=blackpaox,blackpaoy=1,2
black_pao2=blackpao2x,blackpao2y=7,2
black_ma=blackmax,blackmay=7,0
black_ma2=blackma2x,blackma2y=1,0
red_ma=redmax,redmay=1,9
red_ma2=redma2x,redma2y=7,9
red_bing=redbingx,redbingy=6,6
red_bing2=redbing2x,redbing2y=4,6
red_bing3=redbing3x,redbing3y=2,6
red_bing4=redbing4x,redbing4y=0,6
red_bing5=redbing5x,redbing5y=8,6
black_che=blackchex,blackchey=8,0
black_zu=blackzux,blackzuy=0,3
black_zu2=blackzu2x,blackzu2y=2,3
black_zu5=blackzu5x,blackzu5y=4,3
black_zu3=blackzu3x,blackzu3y=6,3
black_zu4=blackzu4x,blackzu4y=8,3
black_che2=blackche2x,blackche2y=0,0
red_che=redchex,redchey=0,9
red_che2=redche2x,redche2y=8,9
black_shi=blackshix,blackshiy=3,0
red_shi=redshix,redshiy=3,9
black_shi2=blackshi2x,blackshi2y=5,0
red_shi2=redshi2x,redshi2y=5,9
black_xiang=blackxiangx,blackxiangy=2,0
red_xiang=redxiangx,redxiangy=2,9
red_xiang2=redxiang2x,redxiang2y=6,9
black_xiang2=blackxiang2x,blackxiang2y=6,0
black_jiang=blackjiangx,blackjiangy=4,0
red_shuai=redshuaix,redshuaiy=4,9
list=[[0]*11 for i in range(10)]
death_value=[]
ischizi_list=[]
chizi=False
zoumajj=False
def ischong(x,z):
    for i in x:
        if i==z:
            return False
    else:
        return True
def ischizi(x,y,z,w):
    if z<list[x][y]<w:
        return True
def getdeathvalue():
    if list[redpaox][redpaoy]!=6:
        if ischong(death_value,6):
            death_value.append(6)
    if list[blackpaox][blackpaoy]!=-6:
        if ischong(death_value,-6):
            death_value.append(-6)
    if list[redpao2x][redpao2y]!=7:
        if ischong(death_value,7):
            death_value.append(7)
    if list[blackpao2x][blackpao2y]!=-7:
        if ischong(death_value,-7):
            death_value.append(-7)
    if list[redmax][redmay]!=9:
        if ischong(death_value,9):
            death_value.append(9)
    if list[blackmax][blackmay]!=-9:
        if ischong(death_value,-9):
            death_value.append(-9)
    if list[blackma2x][blackma2y]!=-15:
        if ischong(death_value,-15):
            death_value.append(-15)
    if list[redma2x][redma2y]!=15:
        if ischong(death_value,15):
            death_value.append(15)
    if list[redbingx][redbingy]!=1:
        if ischong(death_value,1):
            death_value.append(1)
    if list[redbing2x][redbing2y]!=2:
        if ischong(death_value,2):
            death_value.append(2)
    if list[redbing3x][redbing3y]!=3:
        if ischong(death_value,3):
            death_value.append(3)
    if list[redbing4x][redbing4y]!=4:
        if ischong(death_value,4):
            death_value.append(4)
    if list[redbing5x][redbing5y]!=5:
        if ischong(death_value,5):
            death_value.append(5)
    if list[blackzux][blackzuy]!=-1:
        if ischong(death_value,-1):
            death_value.append(-1)
    if list[blackzu2x][blackzu2y]!=-2:
        if ischong(death_value,-2):
            death_value.append(-2)
    if list[blackzu3x][blackzu3y]!=-3:
        if ischong(death_value,-3):
            death_value.append(-3)
    if list[blackzu4x][blackzu4y]!=-4:
        if ischong(death_value,-4):
            death_value.append(-4)
    if list[blackzu5x][blackzu5y]!=-5:
        if ischong(death_value,-5):
            death_value.append(-5)
    if list[blackchex][blackchey]!=-8:
        if ischong(death_value,-8):
            death_value.append(-8)
    if list[blackche2x][blackche2y]!=-16:
        if ischong(death_value,-16):
            death_value.append(-16)
    if list[redchex][redchey]!=8:
        if ischong(death_value,8):
            death_value.append(8)
    if list[redche2x][redche2y]!=16:
        if ischong(death_value,16):
            death_value.append(16)
    if list[blackshix][blackshiy]!=-11:
        if ischong(death_value,-11):
            death_value.append(-11)
    if list[redshix][redshiy]!=11:
        if ischong(death_value,11):
            death_value.append(11)
    if list[redshi2x][redshi2y]!=13:
        if ischong(death_value,13):
            death_value.append(13)
    if list[blackshi2x][blackshi2y]!=-13:
        if ischong(death_value,-13):
            death_value.append(-13)
    if list[blackxiangx][blackxiangy]!=-10:
        if ischong(death_value,-10):
            death_value.append(-10)
    if list[redxiangx][redxiangy]!=10:
        if ischong(death_value,10):
            death_value.append(10)
    if list[redxiang2x][redxiang2y]!=14:
        if ischong(death_value,14):
            death_value.append(14)
    if list[blackxiang2x][blackxiang2y]!=-14:
        if ischong(death_value,-14):
            death_value.append(-14)
    if list[blackjiangx][blackjiangy]!=-12:
        if ischong(death_value,-12):
            death_value.append(-12)
    if list[redshuaix][redshuaiy]!=12:
        if ischong(death_value,-12):
            death_value.append(-12)
def fangqi():
    list[redpaox][redpaoy]=6
    list[blackpaox][blackpaoy]=-6
    list[redpao2x][redpao2y]=7
    list[blackpao2x][blackpao2y]=-7
    list[redmax][redmay]=9
    list[blackmax][blackmay]=-9
    list[blackma2x][blackma2y]=-15
    list[redma2x][redma2y]=15
    list[redbingx][redbingy]=1
    list[redbing2x][redbing2y]=2
    list[redbing3x][redbing3y]=3
    list[redbing4x][redbing4y]=4
    list[redbing5x][redbing5y]=5
    list[blackzux][blackzuy]=-1
    list[blackzu2x][blackzu2y]=-2
    list[blackzu3x][blackzu3y]=-3
    list[blackzu4x][blackzu4y]=-4
    list[blackzu5x][blackzu5y]=-5
    list[blackchex][blackchey]=-8
    list[blackche2x][blackche2y]=-16
    list[redchex][redchey]=8
    list[redche2x][redche2y]=16
    list[blackshix][blackshiy]=-11
    list[redshix][redshiy]=11
    list[redshi2x][redshi2y]=13
    list[blackshi2x][blackshi2y]=-13
    list[blackxiangx][blackxiangy]=-10
    list[redxiangx][redxiangy]=10
    list[redxiang2x][redxiang2y]=14
    list[blackxiang2x][blackxiang2y]=-14
    list[blackjiangx][blackjiangy]=-12
    list[redshuaix][redshuaiy]=12
fangqi()
select=selectx,selecty=-1,-1
left=24
top=20
widthsize=72.4
heightsize=72.3
count=1
biaoji=0
redselectlist=[]
redchesslist=[]
selectlist=[]
# redchesslist=[]
blackselectlist=[]
blackchesslist=[]
chesslist=[]
value_list=[]
chess=-1,-1
tranred=True
running=True
run,start=False,True

def clear():
    global list
    for i in range(9):
        for j in range(10):
            list[i][j]=0  
def sjpm():#帅将碰面
    b=[]#有无其他子
    blackwin=False
    redwin=False
    if blackjiangx==redshuaix:
        if blackjiangy>redshuaiy:
            for j in range(redshuaiy+1,blackjiangy):
                if list[blackjiangx][j]!=0:#有子时填入
                    b.append(j)
        elif blackjiangy<redshuaiy:
            for k in range(blackjiangy+1,redshuaiy):
                if list[blackjiangx][k]!=0:#有子时填入
                    b.append(k)
    if blackjiangx==redshuaix and len(b)==0:
        if biaoji==1:
            blackwin=True
        elif biaoji==2:
            redwin=True       
    return blackwin,redwin

def zoumaj(x,y,jx,jy):
    global zoumajj
    if list[x][y+1]!=0 and list[x][y-1]!=0 and list[x-1][y]!=0 and list[x+1][y]!=0:
         zoumajj=False  
    elif list[x][y-1]!=0 and list[x-1][y]!=0 and list[x+1][y]!=0:
         zoumajj=(jx==x-1  and jy==y+2) or (jx==x+1  and jy==y+2)
    elif list[x][y+1]!=0 and list[x][y-1]!=0 and list[x-1][y]!=0:
         zoumajj=(jx==x+2  and jy==y-1)or (jx==x+2  and jy==y+1)
    elif list[x][y+1]!=0 and list[x-1][y]!=0 and list[x+1][y]!=0:
         zoumajj=(jx==x-1  and jy==y-2) or (jx==x+1  and jy==y-2)
    elif list[x][y+1]!=0 and list[x][y-1]!=0 and list[x+1][y]!=0:
         zoumajj=(jx==x-2  and jy==y+1) or (jx==x-2  and jy==y+1)
    elif list[x][y-1]!=0 and list[x-1][y]!=0:
         zoumajj=(jx==x+2  and jy==y-1)  \
         or (jx==x-2  and jy==y+1)\
         or (jx==x-1  and jy==y+2) or (jx==x+1  and jy==y+2)
    elif list[x-1][y]!=0 and list[x+1][y]!=0:
         zoumajj= (jx==x-1  and jy==y+2) or (jx==x+1  and jy==y+2)\
                or (jx==x-1  and jy==y-2) or (jx==x+1  and jy==y-2)
    elif list[x][y-1]!=0 and list[x][y+1]!=0:
         zoumajj=(jx==x+2  and jy==y-1) or (jx==x-2  and jy==y-1) \
                or (jx==x+2  and jy==y+1) or (jx==x-2  and jy==y+1)                                
    elif list[x][y+1]!=0 and list[x-1][y]!=0:
         zoumajj=(jx==x+2  and jy==y-1)\
         or (jx==x+2  and jy==y+1)\
         or (jx==x-1  and jy==y-2) or (jx==x+1  and jy==y-2) 
    elif list[x][y+1]!=0 and list[x+1][y]!=0:
         zoumajj=(jx==x-2  and jy==y-1) \
         or (jx==x-2  and jy==y+1)\
         or (jx==x-1  and jy==y-2) or (jx==x+1  and jy==y-2)
    elif  list[x+1][y]!=0 and list[x][y-1]!=0:
         zoumajj= (jx==x-2  and jy==y-1)\
         or (jx==x-2  and jy==y+1)\
         or (jx==x-1  and jy==y+2) or (jx==x+1  and jy==y+2)                    
    elif list[x][y-1]!=0:
         zoumajj=(jx==x+2  and jy==y-1) or (jx==x-2  and jy==y-1) \
         or (jx==x+2  and jy==y+1) or (jx==x-2  and jy==y+1)\
         or (jx==x-1  and jy==y+2) or (jx==x+1  and jy==y+2)
    elif list[x][y+1]!=0:
         zoumajj=(jx==x+2  and jy==y-1) or (jx==x-2  and jy==y-1) \
         or (jx==x+2  and jy==y+1) or (jx==x-2  and jy==y+1)\
         or (jx==x-1  and jy==y-2) or (jx==x+1  and jy==y-2)
    elif list[x-1][y]!=0:
         zoumajj=(jx==x+2  and jy==y+1) or (jx==x+2  and jy==y-1)\
         or (jx==x-1  and jy==y+2) or (jx==x+1  and jy==y+2)\
         or (jx==x-1  and jy==y-2) or (jx==x+1  and jy==y-2)
    elif list[x+1][y]!=0:
         zoumajj=(jx==x-2  and jy==y-1) \
         or (jx==x-2  and jy==y+1)\
         or (jx==x-1  and jy==y+2) or (jx==x+1  and jy==y+2)\
         or (jx==x-1  and jy==y-2) or (jx==x+1  and jy==y-2)
    elif list[x][y+1]==0 and list[x][y-1]==0 and list[x-1][y]==0 and list[x+1][y]==0:
        zoumajj = (jx == x + 2 and jy == y - 1) or (jx == x - 2 and jy == y - 1) \
                or (jx == x + 2 and jy == y + 1) or (jx == x - 2 and jy == y + 1) \
                or (jx == x - 1 and jy == y + 2) or (jx == x + 1 and jy == y + 2) \
                or (jx == x - 1 and jy == y - 2) or (jx == x + 1 and jy == y - 2)
    return zoumajj
def zouma(x,y): 
    zouma=(selectx==x+2  and selecty==y-1) or (selectx==x-2  and selecty==y-1) \
    or (selectx==x+2  and selecty==y+1) or (selectx==x-2  and selecty==y+1)\
    or (selectx==x-1  and selecty==y+2) or (selectx==x+1  and selecty==y+2)\
    or (selectx==x-1  and selecty==y-2) or (selectx==x+1  and selecty==y-2) 
    if list[x][y+1]!=0 and list[x][y-1]!=0 and list[x-1][y]!=0 and list[x+1][y]!=0:
         zouma=False  
    elif list[x][y-1]!=0 and list[x-1][y]!=0 and list[x+1][y]!=0:
         zouma=(selectx==x-1  and selecty==y+2) or (selectx==x+1  and selecty==y+2)
    elif list[x][y+1]!=0 and list[x][y-1]!=0 and list[x-1][y]!=0:
         zouma=(selectx==x+2  and selecty==y-1)or (selectx==x+2  and selecty==y+1)
    elif list[x][y+1]!=0 and list[x-1][y]!=0 and list[x+1][y]!=0:
         zouma=(selectx==x-1  and selecty==y-2) or (selectx==x+1  and selecty==y-2)
    elif list[x][y+1]!=0 and list[x][y-1]!=0 and list[x+1][y]!=0:
         zouma=(selectx==x-2  and selecty==y+1) or (selectx==x-2  and selecty==y+1)
    elif list[x][y-1]!=0 and list[x-1][y]!=0:
         zouma=(selectx==x+2  and selecty==y-1)  \
         or (selectx==x-2  and selecty==y+1)\
         or (selectx==x-1  and selecty==y+2) or (selectx==x+1  and selecty==y+2)
    elif list[x-1][y]!=0 and list[x+1][y]!=0:
         zouma= (selectx==x-1  and selecty==y+2) or (selectx==x+1  and selecty==y+2)\
                or (selectx==x-1  and selecty==y-2) or (selectx==x+1  and selecty==y-2)
    elif list[x][y-1]!=0 and list[x][y+1]!=0:
         zouma=(selectx==x+2  and selecty==y-1) or (selectx==x-2  and selecty==y-1) \
                or (selectx==x+2  and selecty==y+1) or (selectx==x-2  and selecty==y+1)                                
    elif list[x][y+1]!=0 and list[x-1][y]!=0:
         zouma=(selectx==x+2  and selecty==y-1)\
         or (selectx==x+2  and selecty==y+1)\
         or (selectx==x-1  and selecty==y-2) or (selectx==x+1  and selecty==y-2) 
    elif list[x][y+1]!=0 and list[x+1][y]!=0:
         zouma=(selectx==x-2  and selecty==y-1) \
         or (selectx==x-2  and selecty==y+1)\
         or (selectx==x-1  and selecty==y-2) or (selectx==x+1  and selecty==y-2)
    elif  list[x+1][y]!=0 and list[x][y-1]!=0:
         zouma= (selectx==x-2  and selecty==y-1)\
         or (selectx==x-2  and selecty==y+1)\
         or (selectx==x-1  and selecty==y+2) or (selectx==x+1  and selecty==y+2)                    
    elif list[x][y-1]!=0:
         zouma=(selectx==x+2  and selecty==y-1) or (selectx==x-2  and selecty==y-1) \
         or (selectx==x+2  and selecty==y+1) or (selectx==x-2  and selecty==y+1)\
         or (selectx==x-1  and selecty==y+2) or (selectx==x+1  and selecty==y+2)
    elif list[x][y+1]!=0:
         zouma=(selectx==x+2  and selecty==y-1) or (selectx==x-2  and selecty==y-1) \
         or (selectx==x+2  and selecty==y+1) or (selectx==x-2  and selecty==y+1)\
         or (selectx==x-1  and selecty==y-2) or (selectx==x+1  and selecty==y-2)
    elif list[x-1][y]!=0:
         zouma=(selectx==x+2  and selecty==y+1) or (selectx==x+2  and selecty==y-1)\
         or (selectx==x-1  and selecty==y+2) or (selectx==x+1  and selecty==y+2)\
         or (selectx==x-1  and selecty==y-2) or (selectx==x+1  and selecty==y-2)
    elif list[x+1][y]!=0:
         zouma=(selectx==x-2  and selecty==y-1) \
         or (selectx==x-2  and selecty==y+1)\
         or (selectx==x-1  and selecty==y+2) or (selectx==x+1  and selecty==y+2)\
         or (selectx==x-1  and selecty==y-2) or (selectx==x+1  and selecty==y-2) 
    return zouma
def zouzu(x,y):
    if y<5:
        zouzu=(selectx==x and selecty==y+1)
    elif y>=5:
        zouzu=(selectx==x and selecty==y+1) or ((selectx==x+1 or selectx==x-1) and selecty==y) 
    return zouzu
def zouzuj(x,y,jx,jy):
    if y<5:
        zouzuj=(jx==x and jy==y+1)
    elif y>=5:
        zouzuj=(jx==x and jy==y+1) or ((jx==x+1 or jx==x-1) and jy==y)
    return zouzuj
def zoubingj(x,y,jx,jy):
    if y>4:
        zoubingj=(jx==x and jy==y-1)
    elif y<=4:
        zoubingj=(jx==x and jy==y-1) or ((jx==x+1 or jx==x-1) and jy==y)
    return zoubingj
def zoubing(x,y):
    if y>4:
        zoubing=(selectx==x and selecty==y-1)
    elif y<=4:
        zoubing=(selectx==x and selecty==y-1) or ((selectx==x+1 or selectx==x-1) and selecty==y) 
    return zoubing
def zuobiao(x,y):
    a=round((x-left-widthsize/2)/widthsize)
    b=round((y-top-heightsize/2)/heightsize)
    return a,b
def xiangzoufa(x,y):
        zouxiang=(selectx==x-2 and selecty==y-2) or (selectx==x-2 and selecty==y+2) \
              or (selectx==x+2 and selecty==y-2) or (selectx==x+2 and selecty==y+2)
        if list[x-1][y-1]!=0 and list[x+1][y-1]!=0 and list[x+1][y+1]!=0 and list[x-1][y+1]!=0:
            zouxiang=False
        elif list[x-1][y-1]!=0 and list[x-1][y+1]!=0 and list[x+1][y-1]!=0:
            zouxiang=(selectx==x+2 and selecty==y+2)    
        elif list[x-1][y-1]!=0 and list[x-1][y+1]!=0 and list[x+1][y+1]!=0:
            zouxiang=(selectx==x+2 and selecty==y-2) 
        elif list[x+1][y-1]!=0 and list[x-1][y+1]!=0 and list[x+1][y+1]!=0:
            zouxiang=(selectx==x-2 and selecty==y-2)  
        elif list[x+1][y-1]!=0 and list[x-1][y-1]!=0 and list[x+1][y+1]!=0:
            zouxiang=(selectx==x-2 and selecty==y+2)
        elif list[x-1][y-1]!=0 and list[x+1][y-1]!=0:
            zouxiang= (selectx==x-2 and selecty==y+2) or (selectx==x+2 and selecty==y+2) 
        elif list[x-1][y-1]!=0 and list[x-1][y+1]!=0:
            zouxiang= (selectx==x+2 and selecty==y-2) or (selectx==x+2 and selecty==y+2)
        elif list[x+1][y+1]!=0 and list[x-1][y+1]!=0:
            zouxiang= (selectx==x-2 and selecty==y-2) or (selectx==x+2 and selecty==y-2)
        elif list[x+1][y+1]!=0 and list[x+1][y-1]!=0:
            zouxiang=(selectx==x-2 and selecty==y-2) or (selectx==x-2 and selecty==y+2)
        elif list[x+1][y+1]!=0 and list[x-1][y-1]!=0:
            zouxiang=(selectx==x+2 and selecty==y-2) or (selectx==x-2 and selecty==y+2)
        elif list[x-1][y+1]!=0 and list[x+1][y-1]!=0:
            zouxiang=(selectx==x+2 and selecty==y+2) or (selectx==x-2 and selecty==y-2)
        elif list[x-1][y-1]!=0:
            zouxiang=(selectx==x-2 and selecty==y+2) or (selectx==x+2 and selecty==y-2) or (selectx==x+2 and selecty==y+2)  
        elif list[x+1][y-1]!=0:
            zouxiang=(selectx==x-2 and selecty==y-2) or (selectx==x-2 and selecty==y+2) \
                      or (selectx==x+2 and selecty==y+2) 
        elif list[x+1][y+1]!=0:
            zouxiang=(selectx==x-2 and selecty==y-2) or (selectx==x-2 and selecty==y+2) \
                    or (selectx==x+2 and selecty==y-2)   
        elif list[x-1][y+1]!=0:
            zouxiang=(selectx==x-2 and selecty==y-2) or (selectx==x+2 and selecty==y-2) or (selectx==x+2 and selecty==y+2)
        return zouxiang  
def zouxiang(x,y):
    zouxiang=(selectx==x-2 and selecty==y-2) or (selectx==x-2 and selecty==y+2) \
              or (selectx==x+2 and selecty==y-2) or (selectx==x+2 and selecty==y+2) 
    if (0<=y<=4 and 0<=x<=8):
        if (0<=selectx<=8 and 0<=selecty<=4):
            zouxiang=xiangzoufa(x,y)
        else:
            zouxiang=False
    elif (5<=y<=9 and 0<=x<=8):
        if (5<=selecty<=9 and 0<=selectx<=8):
            zouxiang=xiangzoufa(x,y)
        else:
            zouxiang=False
    return zouxiang
def chupao(x,y,z,w):
    zoupao = (selectx == x and selecty != y) or (selectx != x and selecty == y)
    if z < list[selectx][selecty] < w:
        if selectx==x and selecty!=y:
            print('吃子')
            zjqz=[]
            if y>selecty:
                for i in range(selecty+1,y):
                    if list[x][i] != 0:
                        zjqz.append(i)
            elif y<selecty:
                for i in range(y+1,selecty):
                    if list[x][i] != 0:
                        zjqz.append(i)
            # print(zjqz)
            if len(zjqz)==1:
                zoupao=True
            else:
                zoupao=False
        elif selecty==y and selectx!=x:
            zjqz2=[]
            if x>selectx:
                for i in range(selectx+1,x):
                    if list[i][y]!=0:
                        zjqz2.append(i)
            elif x<selectx:
                for i in range(x+1,selectx):
                    if list[i][y]!=0:
                        zjqz2.append(i)
            if len(zjqz2)==1:
                zoupao=True
            else:
                zoupao=False
    elif list[selectx][selecty]==0:
        if selectx==x and selecty!=y:
            zjqz=[]
            if y>selecty:
                for i in range(selecty+1,y):
                    print(y)
                    if list[x][i] != 0:
                        zjqz.append(i)
            elif y<selecty:
                for i in range(y+1,selecty):
                    if list[x][i]!=0:
                        zjqz.append(i)
            if len(zjqz)==0:
                zoupao=True
            else:
                zoupao=False

        elif selecty==y and selectx!=x:
            zjqz2=[]
            if x>selectx:
                for i in range(selectx+1,x):
                    if list[i][y]!=0:
                        zjqz2.append(i)
            elif x<selectx:
                for i in range(x+1,selectx):
                    if list[i][y]!= 0:
                        zjqz2.append(i)
            if len(zjqz2)==0:
                zoupao=True
            else:
                zoupao=False
    else:
        zoupao=False
    return zoupao
def chupaoj(x,y,jx,jy):
    if jx==x and jy!=y:
        zjqz=[]
        if y>jy:
            for i in range(jy+1,y):
                if list[x][i] != 0:
                    zjqz.append(i)
        elif y<jy:
            for i in range(y+1,jy):
                if list[x][i] != 0:
                    zjqz.append(i)
        if len(zjqz)==1:
            return True
        else:
            return False
    elif jy==y and jx!=x:
        zjqz2=[]
        if x>jx:
            for i in range(jx+1,x):
                if list[i][y]!=0:
                    zjqz2.append(i)
        elif x<jx:
            for i in range(x+1,jx):
                if list[i][y]!=0:
                    zjqz2.append(i)
        if len(zjqz2)==1:
            return True
        else:
            return False
    else:
        return False
def zoujuj(x,y,jx,jy):
    if jx==x and jy!=y:
        zjqz=[]
        if y>jy:
            for i in range(jy+1,y):
                if list[x][i] != 0:
                    zjqz.append(i)
        elif y<jy:
            for i in range(y+1,jy):
                if list[x][i] != 0:
                    zjqz.append(i)
        if len(zjqz)==0:
            return True
        else:
            return False
    elif jy==y and jx!=x:
        zjqz2=[]
        if x>jx:
            for i in range(jx+1,x):
                if list[i][y]!=0:
                    zjqz2.append(i)
        elif x<jx:
            for i in range(x+1,jx):
                if list[i][y]!=0:
                    zjqz2.append(i)
        if len(zjqz2)==0:
            return True
        else:
            return False
    else:
        return False
def zouju(x,y,z,w):
    zouche = (selectx == x and selecty != y) or (selectx != x and selecty == y)
    if z < list[selectx][selecty] < w:
        if selectx == x and selecty != y:
            print('吃子')
            zjqz = []
            if y > selecty:
                for i in range(selecty + 1, y):
                    if list[x][i] != 0:
                        zjqz.append(i)
            elif y < selecty:
                for i in range(y + 1, selecty):
                    if list[x][i] != 0:
                        zjqz.append(i)
            # print(zjqz)
            if len(zjqz) == 0:
                zouche = True
            else:
                zouche = False
        elif selecty == y and selectx != x:
            zjqz2 = []
            if x > selectx:
                for i in range(selectx + 1, x):
                    if list[i][y] != 0:
                        zjqz2.append(i)
            elif x < selectx:
                for i in range(x + 1, selectx):
                    if list[i][y] != 0:
                        zjqz2.append(i)
            if len(zjqz2) == 0:
                zouche = True
            else:
                zouche = False
    elif list[selectx][selecty] == 0:
        if selectx == x and selecty != y:
            zjqz = []
            if y > selecty:
                for i in range(selecty + 1, y):
                    print(y)
                    if list[x][i] != 0:
                        zjqz.append(i)
            elif y < selecty:
                for i in range(y + 1, selecty):
                    if list[x][i] != 0:
                        zjqz.append(i)
            if len(zjqz) == 0:
                zouche = True
            else:
                zouche = False

        elif selecty == y and selectx != x:
            zjqz2 = []
            if x > selectx:
                for i in range(selectx + 1, x):
                    if list[i][y] != 0:
                        zjqz2.append(i)
            elif x < selectx:
                for i in range(x + 1, selectx):
                    if list[i][y] != 0:
                        zjqz2.append(i)
            if len(zjqz2) == 0:
                zouche = True
            else:
                zouche = False
    else:
        zouche = False
    return zouche

def chupaoj(x, y, jx, jy):
    if jx == x and jy != y:
        zjqz = []
        if y > jy:
            for i in range(jy + 1, y):
                if list[x][i] != 0:
                    zjqz.append(i)
        elif y < jy:
            for i in range(y + 1, jy):
                if list[x][i] != 0:
                    zjqz.append(i)
        if len(zjqz) == 1:
            return True
        else:
            return False
    elif jy == y and jx != x:
        zjqz2 = []
        if x > jx:
            for i in range(jx + 1, x):
                if list[i][y] != 0:
                    zjqz2.append(i)
        elif x < jx:
            for i in range(x + 1, jx):
                if list[i][y] != 0:
                    zjqz2.append(i)
        if len(zjqz2) == 1:
            return True
        else:
            return False
    else:
        return False

def zoushi(x,y):
    if (7<=y<=9 and 3<=x<=5) or (0<=y<=2 and 3<=x<=5):
        if (3<=selectx<=5 and 7<=selecty<=9) or (0<=selecty<=2 and 3<=selectx<=5):
            zoushi=(selectx==x-1 and selecty==y+1) or (selectx==x+1 and selecty==y+1)\
                   or (selectx==x-1 and selecty==y-1) or (selectx==x+1 and selecty==y-1)
        else:
            zoushi=False
        
    return zoushi

def zoushuai(x,y):
    zoushuai=(selectx==x-1 and selecty==y) or (selectx==x+1 and selecty==y)\
            or (selectx==x and selecty==y+1) or (selectx==x and selecty==y-1)
#     a=[]#帅的数
#     b=[]#有无其他子
#     for i in range(9):
#         if selectx!=x:
#             if list[selectx][i]==12 or list[selectx][i]==-12:
#                 a.append(i)
#     if blackjiangy>redshuaiy:
#         for j in range(redshuaiy+1,blackjiangy):
#             if list[selectx][j]!=0:#有子时填入
#                 b.append(j)
#     elif blackjiangy<redshuaiy:
#         for k in range(blackjiangy+1,redshuaiy):
#             if list[selectx][k]!=0:#有子时填入
#                 b.append(k)
    if (7<=y<=9 and 3<=x<=5):
#         if len(a)==1 and len(b)==0:
#             zoushuai=False    
        if (3<=selectx<=5 and 7<=selecty<=9):
            
            zoushuai=(selectx==x-1 and selecty==y) or (selectx==x+1 and selecty==y)\
                   or (selectx==x and selecty==y+1) or (selectx==x and selecty==y-1)
        else:
            zoushuai=False
    elif (0<=y<=2 and 3<=x<=5):
#         if len(a)==1 and len(b)==0:
#               #帅的数为1且中间没子
#             zoushuai=False  
        if (0<=selecty<=2 and 3<=selectx<=5):
            
            zoushuai=(selectx==x-1 and selecty==y) or (selectx==x+1 and selecty==y)\
                   or (selectx==x and selecty==y+1) or (selectx==x and selecty==y-1)
        else:
            zoushuai=False
            
    return zoushuai

def redwin():
    for i in range(9):
        for j in range(10):
            if list[i][j]== 12:
                return False

    else:
        redshuaix,redshuaiy=-1,-1
        return True
def blackwin():
    for i in range(9):
        for j in range(10):
            if list[i][j]==-12:
                return False

    else:
        blackjiangx, blackjiangy = -1, -1
        return True


while True:
    screen.fill(background)
    screen.blit(board,(0,0))
    for i in range(9):
        for j in range(10):
            if list[i][j]==6:
                screen.blit(redpao,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-6:
                screen.blit(blackpao,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==7:
                screen.blit(redpao,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-7:
                screen.blit(blackpao,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-9:
                screen.blit(blackma,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==9:
                screen.blit(redma,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-15:
                screen.blit(blackma,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==15:
                screen.blit(redma,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-16:
                screen.blit(blackche,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==16:
                screen.blit(redche,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==10:
                screen.blit(redxiang,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-10:
                screen.blit(blackxiang,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==8:
                screen.blit(redche,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-8:
                screen.blit(blackche,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==1:
                screen.blit(redbing,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-1:
                screen.blit(blackzu,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==2:
                screen.blit(redbing,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-2:
                screen.blit(blackzu,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==3:
                screen.blit(redbing,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-3:
                screen.blit(blackzu,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==4:
                screen.blit(redbing,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-4:
                screen.blit(blackzu,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==5:
                screen.blit(redbing,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-5:
                screen.blit(blackzu,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==13:
                screen.blit(redshi,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-13:
                screen.blit(blackshi,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==11:
                screen.blit(redshi,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-11:
                screen.blit(blackshi,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==10:
                screen.blit(redxiang,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-10:
                screen.blit(blackxiang,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==14:
                screen.blit(redxiang,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-14:
                screen.blit(blackxiang,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==-12:
                screen.blit(blackjiang,(left+widthsize*i,top+heightsize*j))
            if list[i][j]==12:
                screen.blit(redshuai,(left+widthsize*i,top+heightsize*j))
    for event in pg.event.get():
        if event.type==pg.QUIT:
            root.destroy()
            pg.quit()
            os._exit(1)
        elif event.type==pg.MOUSEBUTTONDOWN:
            x,y=event.pos
            if 0<x<width-left and 0<y<height-top:
                select=selectx,selecty=zuobiao(x,y)
                if running:
                    if tranred:
                        if event.button==3:#右键重选
                            count=1
                        if list[selectx][selecty]>0 and event.button==1:#左键选择棋子
                            if count==1:
                                biaoji=1
                                redselectlist.append(select)
                                count+=1
                        elif count==2 and event.button==1:
                            button2.config(state=tkinter.DISABLED)
                            button3.config(state=tkinter.DISABLED)
                            button4.config(state=tkinter.DISABLED)
                            #落子
                            if ischizi(selectx,selecty,-17,0):
                                chizi=True
                            else:
                                chizi=False
                                death_value.append(0)
                            ischizi_list.append(chizi)
                            if redselectlist!=[]:
                                if list[redselectlist[-1][0]][redselectlist[-1][1]]==6 and chupao(redpaox,redpaoy,-17,0):#炮
                                    selectlist.append((redpaox,redpaoy))
                                    list[redpaox][redpaoy]=0
                                    redpaox,redpaoy=selectx,selecty
                                    list[redpaox][redpaoy]=6
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(6)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==7 and chupao(redpao2x,redpao2y,-17,0):#炮
                                    selectlist.append((redpao2x,redpao2y))
                                    list[redpao2x][redpao2y]=0
                                    redpao2x,redpao2y=selectx,selecty
                                    list[redpao2x][redpao2y]=7
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(7)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==3 and zoubing(redbing3x,redbing3y):
                                    selectlist.append((redbing3x,redbing3y))
                                    list[redbing3x][redbing3y]=0
                                    redbing3x,redbing3y=selectx,selecty
                                    list[redbing3x][redbing3y]=3
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(3)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==4 and zoubing(redbing4x,redbing4y):
                                    selectlist.append((redbing4x, redbing4y))
                                    list[redbing4x][redbing4y]=0
                                    redbing4x,redbing4y=selectx,selecty
                                    list[redbing4x][redbing4y]=4
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(4)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==5 and zoubing(redbing5x,redbing5y):
                                    selectlist.append((redbing5x, redbing5y))
                                    list[redbing5x][redbing5y]=0
                                    redbing5x,redbing5y=selectx,selecty
                                    list[redbing5x][redbing5y]=5
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(5)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==1 and zoubing(redbingx,redbingy):
                                    selectlist.append((redbingx, redbingy))
                                    list[redbingx][redbingy]=0
                                    redbingx,redbingy=selectx,selecty
                                    list[redbingx][redbingy]=1
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(1)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==2 and zoubing(redbing2x,redbing2y):
                                    selectlist.append((redbing2x, redbing2y))
                                    list[redbing2x][redbing2y]=0
                                    redbing2x,redbing2y=selectx,selecty
                                    list[redbing2x][redbing2y]=2
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(2)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==12 and zoushuai(redshuaix,redshuaiy):
                                    selectlist.append((redshuaix, redshuaiy))
                                    list[redshuaix][redshuaiy]=0
                                    redshuaix,redshuaiy=selectx,selecty
                                    list[redshuaix][redshuaiy]=12
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(12)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==9 and zouma(redmax,redmay):
                                    selectlist.append((redmax, redmay))
                                    list[redmax][redmay]=0
                                    redmax,redmay=selectx,selecty
                                    list[redmax][redmay]=9
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(9)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==15 and zouma(redma2x,redma2y):
                                    selectlist.append((redma2x, redma2y))
                                    list[redma2x][redma2y]=0
                                    redma2x,redma2y=selectx,selecty
                                    list[redma2x][redma2y]=15
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(15)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==8 and zouju(redchex,redchey,-17,0):
                                    selectlist.append((redchex,redchey))
                                    list[redchex][redchey]=0
                                    redchex,redchey=selectx,selecty
                                    list[redchex][redchey]=8
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(8)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==16 and zouju(redche2x,redche2y,-17,0):
                                    selectlist.append((redche2x, redche2y))
                                    list[redche2x][redche2y]=0
                                    redche2x,redche2y=selectx,selecty
                                    list[redche2x][redche2y]=16
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(16)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==11 and zoushi(redshix,redshiy):
                                    selectlist.append((redshix, redshiy))
                                    list[redshix][redshiy]=0
                                    redshix,redshiy=selectx,selecty
                                    list[redshix][redshiy]=11
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(11)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==13 and zoushi(redshi2x,redshi2y):
                                    selectlist.append((redshi2x, redshi2y))
                                    list[redshi2x][redshi2y]=0
                                    redshi2x,redshi2y=selectx,selecty
                                    list[redshi2x][redshi2y]=13
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(13)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==10 and zouxiang(redxiangx,redxiangy):
                                    selectlist.append((redxiangx,redxiangy))
                                    list[redxiangx][redxiangy]=0
                                    redxiangx,redxiangy=selectx,selecty
                                    list[redxiangx][redxiangy]=10
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(10)
                                    count=1
                                    tranred=False
                                elif list[redselectlist[-1][0]][redselectlist[-1][1]]==14 and zouxiang(redxiang2x,redxiang2y):
                                    selectlist.append((redxiang2x, redxiang2y))
                                    list[redxiang2x][redxiang2y]=0
                                    redxiang2x,redxiang2y=selectx,selecty
                                    list[redxiang2x][redxiang2y]=14
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(14)
                                    count=1
                                    tranred=False
                                getdeathvalue()
                                print(death_value)
                                print(chesslist,selectlist,value_list)
                    else:

                        if event.button==3:#右键重选
                            count=1
                        if list[selectx][selecty]<0 and event.button==1:#左键选择=
                            if count==1:
                                biaoji=2
                                blackselectlist.append(select)
                                count+=1
                        elif count==2 and event.button==1:#落子          
                            button2.config(state=tkinter.DISABLED)
                            button3.config(state=tkinter.DISABLED)
                            button4.config(state=tkinter.DISABLED)
                            if ischizi(selectx,selecty,0,17):
                                chizi = True
                            else:
                                chizi = False
                                death_value.append(0)
                            ischizi_list.append(chizi)
                            if blackselectlist!=[]:
                                if list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-6 and chupao(blackpaox,blackpaoy,0,17):#炮
                                    selectlist.append((blackpaox, blackpaoy))
                                    list[blackpaox][blackpaoy]=0
                                    blackpaox,blackpaoy=selectx,selecty
                                    list[blackpaox][blackpaoy]=-6
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-6)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-7 and chupao(blackpao2x,blackpao2y,0,17):#炮
                                    selectlist.append((blackpao2x, blackpao2y))
                                    list[blackpao2x][blackpao2y]=0
                                    blackpao2x,blackpao2y=selectx,selecty
                                    list[blackpao2x][blackpao2y]=-7
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-7)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-3 and zouzu(blackzu3x,blackzu3y):
                                    selectlist.append((blackzu3x, blackzu3y))
                                    list[blackzu3x][blackzu3y]=0
                                    blackzu3x,blackzu3y=selectx,selecty
                                    list[blackzu3x][blackzu3y]=-3
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-3)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-4 and zouzu(blackzu4x,blackzu4y):
                                    selectlist.append((blackzu4x, blackzu4y))
                                    list[blackzu4x][blackzu4y]=0
                                    blackzu4x,blackzu4y=selectx,selecty
                                    list[blackzu4x][blackzu4y]=-4
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-4)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-5 and zouzu(blackzu5x,blackzu5y):
                                    selectlist.append((blackzu5x, blackzu5y))
                                    list[blackzu5x][blackzu5y]=0
                                    blackzu5x,blackzu5y=selectx,selecty
                                    list[blackzu5x][blackzu5y]=-5
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-5)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-1 and zouzu(blackzux,blackzuy):
                                    selectlist.append((blackzux, blackzuy))
                                    list[blackzux][blackzuy]=0
                                    blackzux,blackzuy=selectx,selecty
                                    list[blackzux][blackzuy]=-1
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-1)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-2 and zouzu(blackzu2x,blackzu2y):
                                    selectlist.append((blackzu2x, blackzu2y))
                                    list[blackzu2x][blackzu2y]=0
                                    blackzu2x,blackzu2y=selectx,selecty
                                    list[blackzu2x][blackzu2y]=-2
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-2)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-12 and zoushuai(blackjiangx,blackjiangy):
                                    selectlist.append((blackjiangx, blackjiangy))
                                    list[blackjiangx][blackjiangy]=0
                                    blackjiangx,blackjiangy=selectx,selecty
                                    list[blackjiangx][blackjiangy]=-12
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-12)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-9 and zouma(blackmax,blackmay):
                                    selectlist.append((blackmax, blackmay))
                                    list[blackmax][blackmay]=0
                                    blackmax,blackmay=selectx,selecty
                                    list[blackmax][blackmay]=-9
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-9)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-15 and zouma(blackma2x,blackma2y):
                                    selectlist.append((blackma2x, blackma2y))
                                    list[blackma2x][blackma2y]=0
                                    blackma2x,blackma2y=selectx,selecty
                                    list[blackma2x][blackma2y]=-15
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-15)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-8 and zouju(blackchex,blackchey,0,17):
                                    selectlist.append((blackchex, blackchey))
                                    list[blackchex][blackchey]=0
                                    blackchex,blackchey=selectx,selecty
                                    list[blackchex][blackchey]=-8
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-8)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-16 and zouju(blackche2x,blackche2y,0,17):
                                    selectlist.append((blackche2x, blackche2y))
                                    list[blackche2x][blackche2y]=0
                                    blackche2x,blackche2y=selectx,selecty
                                    list[blackche2x][blackche2y]=-16
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-16)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-11 and zoushi(blackshix,blackshiy):
                                    selectlist.append((blackshix, blackshiy))
                                    list[blackshix][blackshiy]=0
                                    blackshix,blackshiy=selectx,selecty
                                    list[blackshix][blackshiy]=-11
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-11)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-13 and zoushi(blackshi2x,blackshi2y):
                                    selectlist.append((blackshi2x, blackshi2y))
                                    list[blackshi2x][blackshi2y]=0
                                    blackshi2x,blackshi2y=selectx,selecty
                                    list[blackshi2x][blackshi2y]=-13
                                    value_list.append(-13)
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-10 and zouxiang(blackxiangx,blackxiangy):
                                    selectlist.append((blackxiangx,blackxiangy))
                                    list[blackxiangx][blackxiangy]=0
                                    blackxiangx,blackxiangy=selectx,selecty
                                    list[blackxiangx][blackxiangy]=-10
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-10)
                                    count=1
                                    tranred=True
                                elif list[blackselectlist[-1][0]][blackselectlist[-1][1]]==-14 and zouxiang(blackxiang2x,blackxiang2y):
                                    selectlist.append((blackxiang2x, blackxiang2y))
                                    list[blackxiang2x][blackxiang2y]=0
                                    blackxiang2x,blackxiang2y=selectx,selecty
                                    list[blackxiang2x][blackxiang2y]=-14
                                    chess=selectx,selecty
                                    chesslist.append(chess)
                                    value_list.append(-14)
                                    count=1
                                    tranred=True
                                getdeathvalue()
                                print(death_value)
                                print(chesslist, selectlist, value_list)
            if 720<x<width2 and 500<y<580:
                pg.quit()
                sys.exit()
            if 720<x<width2 and 100<y<180 and running: #只能悔棋一次
                if chesslist!=[] and selectlist!=[] and value_list!=[]:
                    xiazix,xiaziy=chesslist[-1][0],chesslist[-1][1]
                    yuanx,yuany=selectlist[-1][0],selectlist[-1][1]
                    if ischizi_list[-1]:
                        list[xiazix][xiaziy] = death_value[-1]
                    else:
                        list[xiazix][xiaziy]=0
                    print(ischizi_list)
                    print(selectlist)
                    print(yuanx,yuany)
                    print(value_list)
                    list[yuanx][yuany]=value_list[-1]
                    if value_list[-1]==6:
                        redpaox,redpaoy=yuanx,yuany
                    elif value_list[-1]==-6:
                        blackpaox,blackpaoy=yuanx,yuany
                    elif value_list[-1]==7:
                        redpao2x,redpao2y=yuanx,yuany
                    elif value_list[-1]==-7:
                        blackpao2x,blackpao2y=yuanx,yuany
                    elif value_list[-1]==9:
                        redmax,redmay=yuanx,yuany
                    elif value_list[-1]==-9:
                        blackmax,blackmay=yuanx,yuany
                    elif value_list[-1]==15:
                        redma2x,redma2y=yuanx,yuany
                    elif value_list[-1]==-15:
                        blackma2x,blackma2y=yuanx,yuany
                    elif value_list[-1]==1:
                        redbingx,redbingy=yuanx,yuany
                    elif value_list[-1]==-1:
                        blackzux,blackzuy=yuanx,yuany
                    elif value_list[-1]==2:
                        redbing2x,redbing2y=yuanx,yuany
                    elif value_list[-1]==-2:
                        blackzu2x,blackzu2y=yuanx,yuany
                    elif value_list[-1]==3:
                        redbing3x,redbing3y=yuanx,yuany
                    elif value_list[-1]==-3:
                        blackzu3x,blackzu3y=yuanx,yuany
                    elif value_list[-1]==4:
                        redbing4x,redbing4y=yuanx,yuany
                    elif value_list[-1]==-4:
                        blackzu4x,blackzu4y=yuanx,yuany
                    elif value_list[-1]==5:
                        redbing5x,redbing5y=yuanx,yuany
                    elif value_list[-1]==-5:
                        blackzu5x,blackzu5y=yuanx,yuany
                    elif value_list[-1]==8:
                        redchex,redchey=yuanx,yuany
                    elif value_list[-1]==-8:
                        blackchex,blackchey=yuanx,yuany
                    elif value_list[-1]==16:
                        redche2x,redche2y=yuanx,yuany
                    elif value_list[-1]==-16:
                        blackche2x,blackche2y=yuanx,yuany
                    elif value_list[-1]==11:
                        redshix,redshiy=yuanx,yuany
                    elif value_list[-1]==-11:
                        blackshix,blackshiy=yuanx,yuany
                    elif value_list[-1]==13:
                        redshi2x,redshi2y=yuanx,yuany
                    elif value_list[-1]==-13:
                        blackshi2x,blackshi2y=yuanx,yuany
                    elif value_list[-1]==10:
                        redxiangx,redxiangy=yuanx,yuany
                    elif value_list[-1]==-10:
                        blackxiangx,blackxiangy=yuanx,yuany
                    elif value_list[-1]==14:
                        redxiang2x,redxiang2y=yuanx,yuany
                    elif value_list[-1]==-14:
                        blackxiang2x,blackxiang2y=yuanx,yuany
                    elif value_list[-1]==12:
                        redshuaix,redshuaiy=yuanx,yuany
                    elif value_list[-1]==-12:
                        blackjiangx,blackjiangy=yuanx,yuany
                    if value_list!=[]:
                        value_list.pop()
                    if selectlist!=[]:
                        selectlist.pop()
                    if chesslist!=[]:
                        chesslist.pop()
                    if ischizi_list!=[]:
                        ischizi_list.pop()
                    if death_value!=[]:
                        death_value.pop()
                    tranred = not tranred
                else:
                    print('列表为空')

            if 720<x<width2 and 300<y<380:
                button4.config(state=tkinter.NORMAL)
                button2.config(state=tkinter.NORMAL)
                button3.config(state=tkinter.NORMAL)
                step=0
                countqipu=0
                ischizi_list.clear()
                value_list.clear()
                death_value.clear()
                selectlist.clear()
                chesslist.clear()
                clear()
                redselectlist.clear()
                blackselectlist.clear()
                # redchesslist.clear()
                # blackchesslist.clear()
                tranred=True
                running=True
                redpaox,redpaoy=1,7
                redpao2x,redpao2y=7,7
                blackpaox,blackpaoy=1,2
                blackpao2x,blackpao2y=7,2
                blackmax,blackmay=7,0
                blackma2x,blackma2y=1,0
                redmax,redmay=1,9
                redma2x,redma2y=7,9
                redbingx,redbingy=6,6
                redbing2x,redbing2y=4,6
                redbing3x,redbing3y=2,6
                redbing4x,redbing4y=0,6
                redbing5x,redbing5y=8,6
                blackchex,blackchey=8,0
                blackzux,blackzuy=0,3
                blackzu2x,blackzu2y=2,3
                blackzu5x,blackzu5y=4,3
                blackzu3x,blackzu3y=6,3
                blackzu4x,blackzu4y=8,3
                blackche2x,blackche2y=0,0
                redchex,redchey=0,9
                redche2x,redche2y=8,9
                blackshix,blackshiy=3,0
                redshix,redshiy=3,9
                blackshi2x,blackshi2y=5,0
                redshi2x,redshi2y=5,9
                blackxiangx,blackxiangy=2,0
                redxiangx,redxiangy=2,9
                redxiang2x,redxiang2y=6,9
                blackxiang2x,blackxiang2y=6,0
                blackjiangx,blackjiangy=4,0
                redshuaix,redshuaiy=4,9
                fangqi()
    if  list[selectx][selecty]>0 and tranred:
        pg.draw.rect(screen,(255,0,0),[selectx*widthsize+23,selecty*heightsize+20,65,65],2)
    elif list[selectx][selecty]<0 and tranred==False:
        pg.draw.rect(screen,(0,0,0),[selectx*widthsize+23,selecty*heightsize+20,65,65],2)
    if sjpm()[0]:
        if not blackwin():
            Font=font.render("黑棋赢", True, (0,0,0))
            screen.blit(Font,(width/2-80,height/2-30))
            writeqipu()
            running=False
    elif sjpm()[1]:
        if not redwin():
            Font=font.render("红棋赢", True, (255,0,0))
            screen.blit(Font,(width/2-80,height/2-30))
            writeqipu()
            running=False
    if redwin():
        Font=font.render("黑棋赢", True, (0,0,0))
        screen.blit(Font,(width/2-80,height/2-30))
        writeqipu()
        running=False
    elif blackwin():
        Font=font.render("红棋赢", True, (255,0,0))
        screen.blit(Font,(width/2-80,height/2-30))
        writeqipu()
        running=False
    redpaoj = chupaoj(redpaox, redpaoy,blackjiangx,blackjiangy)
    redpaoj2 = chupaoj(redpao2x, redpao2y,blackjiangx,blackjiangy)
    redbingj=zoubingj(redbingx,redbingy,blackjiangx,blackjiangy)
    redbingj2 = zoubingj(redbing2x, redbing2y, blackjiangx, blackjiangy)
    redbingj3= zoubingj(redbing3x, redbing3y, blackjiangx, blackjiangy)
    redbingj4= zoubingj(redbing4x, redbing4y, blackjiangx, blackjiangy)
    redbingj5 = zoubingj(redbing5x, redbing5y, blackjiangx, blackjiangy)
    redmaj=zoumaj(redmax,redmay,blackjiangx,blackjiangy)
    redmaj2 = zoumaj(redma2x, redma2y, blackjiangx, blackjiangy)
    redchej=zoujuj(redchex,redchey,blackjiangx,blackjiangy)
    redchej2 = zoujuj(redche2x, redche2y, blackjiangx, blackjiangy)
    blackpaoj = chupaoj(blackpaox, blackpaoy,redshuaix,redshuaiy)
    blackpaoj2 = chupaoj(blackpao2x, blackpao2y,redshuaix,redshuaiy)
    blackmaj=zoumaj(blackmax,blackmay,redshuaix,redshuaiy)
    blackmaj2 = zoumaj(blackma2x, blackma2y,redshuaix,redshuaiy)
    blackchej=zoujuj(blackchex,blackchey,redshuaix,redshuaiy)
    blackchej2 = zoujuj(blackche2x, blackche2y,redshuaix,redshuaiy)
    zuj=zouzuj(blackzux,blackzuy,redshuaix,redshuaiy)
    zuj2 = zouzuj(blackzu2x, blackzu2y, redshuaix, redshuaiy)
    zuj3 = zouzuj(blackzu3x, blackzu3y, redshuaix, redshuaiy)
    zuj4 = zouzuj(blackzu4x, blackzu4y, redshuaix, redshuaiy)
    zuj5 = zouzuj(blackzu5x, blackzu5y, redshuaix, redshuaiy)
    flag=(redpaoj or redpaoj2 or redmaj or redmaj2 or redchej or redchej2 or redbingj or redbingj2 or redbingj3 or redbingj4
          or redbingj5)
    flag2=(blackpaoj or blackpaoj2 or blackmaj or blackmaj2 or blackchej or blackchej2 or zuj or zuj2 or zuj3 or zuj4 or
           zuj5)
    if flag and not tranred:
        if not blackwin() and not sjpm()[0]:
            Font=font.render('将军', True, (255,0,0))
            screen.blit(Font,(width/2-65,height/2-35))
    if flag2 and tranred:
        if not redwin() and not sjpm()[1]:
            Font=font.render('将军', True, (0,0,0))
            screen.blit(Font,(width/2-65,height/2-35))
    text2=font2.render("悔棋", True, (0,0,0))
    text1=font3.render("重新开始", True, (0,0,0))
    text3=font3.render("退出游戏", True, (0,0,0))
    pg.draw.rect(screen,(0,0,0),[720,100,150,80],2)
    screen.blit(text2,(745,115))
    pg.draw.rect(screen,(0,0,0),[720,300,170,80],2)
    screen.blit(text1,(725,325))
    pg.draw.rect(screen,(0,0,0),[720,500,170,80],2)
    screen.blit(text3,(725,525))
    pg.display.flip()
    try:
        root.update()
    except:
        pass

